package com.wxhandle.cleandemo.oauth.service;

import javax.transaction.Transactional;

import com.wxhandle.cleandemo.core.domain.identity.SysUser;
import com.wxhandle.cleandemo.core.services.SysUserService;
import com.wxhandle.cleandemo.oauth.model.CarbonUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CarbonUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserService userService;
    
    @Override
    @Transactional
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        
		// PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        log.info("get login user begin.");
        SysUser user = userService.getOneByLogin(username);
        user.getRoles();
        log.info("get login user finish.");
        return CarbonUser.of(user);
    }
    
}
